<?php

class Setting {
  /**
   * @var array
   */
  protected static $_data;

   /**
   * execute boot
   */
  protected static function _boot() {
    $db = db::factory();
    $table = db::get_tablename('core_setting');
    $sql = "select name, value from $table where boot = 1;";
    self::$_data = $db->fetchPairs($sql);
  }

  /**
   * check boot
   */
  public static function boot() {
    if (self::$_data === NULL) {
      self::_boot();
    }
  }

  /**
   * load all param in the same group with name
   * @param  string $name
   * @param  mixed  $default  optional
   * @return mixed
   */
  protected static function _load($name, $default =  NULL) {
    $db = db::factory();
    $table = db::get_tablename('core_setting');
    $sql = "
      select name, value from $table
      where scope =
      (select scope from $table where name='user.cookie_name') and boot= 0;";

    if ($rows = $db->fetchRow($sql)) {
      foreach ($rows as $row) {
        self::$_data[$row['name']] = $row['value'];
      }
    }
    return @isset(self::$_data[$name]);
  }

  /**
   *
   * @param  string $name
   * @return mixed
   */
  public static function get($name, $default = NULL) {
    return @isset(self::$_data[$name]) ? self::$_data[$name]:(self::_load($name) ? self::$_data[$name] : (self::$_data[$name] = $default));
  }

}
//setting::boot();